/*
 * Copyright 2020 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package cc.siyecao.mapper.batch;

import cc.siyecao.mapper.provider.Caching;
import org.apache.ibatis.annotations.*;

import java.util.Collection;
import java.util.List;

/**
 * 批量操作方法
 *
 * @param <T> 实体类类型
 * @author liuzh
 */
public interface BatchMapper<T, ID> {

    /**
     * 批量保存实体，需要数据库支持批量插入的语法
     *
     * @param entityList 实体列表
     * @return 结果数等于 entityList.size() 时成功，不相等时失败
     */
    @Lang(Caching.class)
    @InsertProvider(type = BatchProvider.class, method = "insertBatch")
    int insertBatch(@Param("entityList") List<T> entityList);

    /**
     * 批量保存实体(非空字段)，需要数据库支持批量插入的语法
     *
     * @param entityList 实体列表
     * @return 结果数等于 entityList.size() 时成功，不相等时失败
     */
    @Lang(Caching.class)
    @InsertProvider(type = BatchProvider.class, method = "insertBatchSelective")
    int insertBatchSelective(@Param("entityList") List<T> entityList);

    /**
     * 批量更新实体，需要数据库支持批量插入的语法
     *
     * @param entityList 实体列表
     * @return 结果数等于 entityList.size() 时成功，不相等时失败
     */
    @Lang(Caching.class)
    @UpdateProvider(type = BatchProvider.class, method = "updateBatch")
    int updateBatch(@Param("entityList") List<T> entityList);

    /**
     * 批量更新实体(非空字段)，需要数据库支持批量插入的语法
     *
     * @param entityList 实体列表
     * @return 结果数等于 entityList.size() 时成功，不相等时失败
     */
    @Lang(Caching.class)
    @UpdateProvider(type = BatchProvider.class, method = "updateBatchSelective")
    int updateBatchSelective(@Param("entityList") List<T> entityList);

    /**
     * 根据ID批量删除实体，需要数据库支持批量插入的语法
     *
     * @param idList 实体列表
     * @return 结果数等于 idList.size() 时成功，不相等时失败
     */
    @Lang(Caching.class)
    @DeleteProvider(type = BatchProvider.class, method = "deleteBatchIds")
    int deleteBatchIds(@Param("idList") Collection<ID> idList);

    /**
     * 根据ID批量查询实体，需要数据库支持批量插入的语法
     *
     * @param idList 实体列表
     * @return 结果数等于 idList.size() 时成功，不相等时失败
     */
    @Lang(Caching.class)
    @SelectProvider(type = BatchProvider.class, method = "selectByIds")
    List<T> selectByIds(@Param("idList") Collection<ID> idList);

}
